package com.fadadianqi.logistics.admin.staff.controller;

import com.fadadianqi.logistics.admin.controller.form.AjaxResult;
import com.fadadianqi.logistics.admin.staff.model.Action;
import com.fadadianqi.logistics.admin.staff.model.Group;
import com.fadadianqi.logistics.admin.staff.service.ActionService;
import com.fadadianqi.logistics.admin.staff.service.GroupActionService;
import com.fadadianqi.logistics.admin.staff.service.GroupService;
import com.fadadianqi.commons.JSONProvider;
import com.fadadianqi.commons.ParamHelper;
import com.yeqiangwei.commons.util.BeanUtils;
import com.yeqiangwei.commons.util.ParamUtils;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限组管理
 *
 * @author aroot
 */
@Controller
public class GroupController {


    @RequestMapping(value = "/a/staff/group/form", method = RequestMethod.GET)
    public String showForm(HttpServletRequest request, ModelMap modelMap) {

        String gpid = ParamHelper.getId(request, null);
        Group group = groupService.getById(gpid);
        modelMap.put("group", group);

        List<Action> gActions = groupActionService.findByGroup(group);
        modelMap.put("gActions", gActions);

        List<Action> actions = actionService.findAll();
        modelMap.put("actions", actions);


        return "/admin/staff/groupForm";
    }

    @RequestMapping(value = "/a/ajax/staff/group/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> save(HttpServletRequest request, ModelMap modelMap) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        AjaxResult rs = null;

        String name = ParamHelper.getName(request, null);
        String id = ParamHelper.getId(request, null);

        Group group = new Group();
        group.setId(id);
        group.setName(name);
        groupService.save(group);

        String _aids = ParamUtils.getString(request, "aids", null);
        if (ValidatorHelper.isNotEmpty(_aids)) {
            /*List<Action> _buf = groupActionService.findByGroup(group);
			Set<String> buf = new HashSet<String>();
			if(ValidatorHelper.isNotEmpty(_buf)) {
				for(Action a : _buf) {
					buf.add(a.getId());
				}
			}
			*/
            groupActionService.delByGroup(group);
            String[] aids = _aids.split(",");
            for (String aid : aids) {
                Action a = actionService.getById(aid);
                groupActionService.actionToGroup(a, group);
            }
        } else {
            // 删除全部
            groupActionService.delByGroup(group);
        }
        rs = new AjaxResult(group, Boolean.TRUE, null);
        return new ResponseEntity<String>(JSONProvider.toJSON(rs),
                responseHeaders, HttpStatus.OK);
    }

    /**
     * 显示用户组以及组所拥有的权限
     *
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/a/staff/group/list", method = RequestMethod.GET)
    public String showList(HttpServletRequest request, ModelMap modelMap) {
        List<Group> _groups = groupService.findAll();
        List<GroupVO> groups = new ArrayList<GroupVO>();
        for (Group group : _groups) {
            GroupVO vo = new GroupVO();
            BeanUtils.copyProperties(vo, group);
            List<Action> actions = groupActionService.findByGroup(group);
            vo.setActions(actions);
            groups.add(vo);
        }
        modelMap.put("groups", groups);
        return "/admin/staff/groupList";
    }

    @Autowired
    private GroupService groupService;

    @Autowired
    private GroupActionService groupActionService;

    @Autowired
    private ActionService actionService;

    public static class GroupVO extends Group {

        /**
         *
         */
        private static final long serialVersionUID = -8891149540464002038L;


        private List<Action> actions;


        public List<Action> getActions() {
            return actions;
        }

        public void setActions(List<Action> actions) {
            this.actions = actions;
        }
    }
}
